#ifndef CLIENT_H
#define CLIENT_H

#include "utilities.h"
#include "clientgame.h"
#include "render.h"
#include <QObject>
#include <QtNetwork>

class Client : public QObject
{
   Q_OBJECT
public:
   explicit Client(QObject *parent = 0);
   virtual ~Client();

   void sendPlayerJoin(const QString &address, int port);
   void sendPlayerReady(const bool ready=true);
   void sendEntityAdd(const EntityBaseStats &stats, const QPointF &position);
   void sendEntitySquadAdd(const EntitySquadType &squad, const QPointF &position);
   void sendEntitySquadAddToEmitter(const EntitySquadType &squad, const quint32 emitterID);
   void sendEntitySquadIDRequest();
   //void sendEntityModify();

   void setRender(Render *render);

   ClientGame *game;

signals:
   void newSquadID(quint32 squadID);
   void squadRemoved(quint32 squadID);

public slots:
   void onData();
   void onRenderTimer();
   void onDisplayError(QAbstractSocket::SocketError socketError);

private:
   void processJoin(QDataStream &data);
   void processLeave(QDataStream &data);
   void processGameEvent(QDataStream &data);

   void processMapChange(QDataStream &data);
   void processEntityUpdates(QDataStream &data);
   void processEntityAdd(QDataStream &data);
   void processEntityAttack(QDataStream &data);
   void processEntityRemove(QDataStream &data);
   void processEntitySquadRemove(QDataStream &data);
   void processEntitySquadIDRequest(QDataStream &data);
   void processEntitySquadAddToEmitter(QDataStream &data);
   void processEntitySquadRemoveFromEmitter(QDataStream &data);

   QTcpSocket *mTcpSocket;
   int mHostPort;
   quint16 mBlockSize;
   QHostAddress mHostAddress;
   int mClientPort;
   bool mIsJoined;
   int mPlayerID;
   Render *mRender;
   QTimer *mRenderTimer;
};

#endif // CLIENT_H
